package com.tiger.cloud.service.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tiger.cloud.common.view.SpringUtils;
import com.tiger.cloud.model.crm.Regions;
import com.tiger.cloud.service.crm.IRegionsService;

/**
 * 区域缓存
 */
public final class CommonRegions {

	//省
	private volatile static List<Regions> provices =null;
	//市
	private volatile static Map<Integer, List<Regions>> citys = new HashMap<Integer,List<Regions>>();
	//区
	private volatile static Map<Integer, List<Regions>> districts = new HashMap<Integer,List<Regions>>();

	/**
	 * 初始化加载字典
	 */
	public static void init() {
		IRegionsService iRegionsService = SpringUtils.getBean(IRegionsService.class);
		Map<String, Object> findProvicesMaps = new HashMap<String, Object>();
		findProvicesMaps.put("regionGrade",1);
		provices = iRegionsService.find(findProvicesMaps);
		
		findProvicesMaps = new HashMap<String, Object>();
		findProvicesMaps.put("regionGrade",2);
		List<Regions> cityData = iRegionsService.find(findProvicesMaps);
		Integer provice=null;
		for (Regions region : cityData) {
			if (region.getParentRegionCode()!=null) {
				if (provice==null || provice!=region.getParentRegionCode()) {
					List<Regions> list = new ArrayList<Regions>();
					list.add(region);
					citys.put(region.getParentRegionCode(), list);
				} else {
					citys.get(region.getParentRegionCode()).add(region);
				}
			}
			provice = region.getParentRegionCode();
		}
		
		findProvicesMaps = new HashMap<String, Object>();
		findProvicesMaps.put("regionGrade",3);
		List<Regions> districtData = iRegionsService.find(findProvicesMaps);
		Integer cityId=null;
		for (Regions region : districtData) {
			if (region.getParentRegionCode()!=null) {
				if (cityId==null || cityId!=region.getParentRegionCode()) {
					List<Regions> list = new ArrayList<Regions>();
					list.add(region);
					districts.put(region.getParentRegionCode(), list);
				} else {
					districts.get(region.getParentRegionCode()).add(region);
				}
			}
			cityId = region.getParentRegionCode();
		}
	}

	
	public static List<Regions> getProvices() {
		return provices;
	}

	public static List<Regions> getCitys(Long proviceId) {
		return citys.get(proviceId);
	}

	public static List<Regions> getDistricts(Long cityId) {
		return districts.get(cityId);
	}

	
}
